package com.stations.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.stations.domain.TTransmitter;
import com.stations.domain.TTransmitterProgram;
import com.stations.domain.TTransmitterTask;
import com.stations.mapper.TTransmitterProgramMapper;
import com.stations.service.TransmitterSendService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * ProgramTask
 *
 * @author jintao
 * @version 2023/10/12 14:56
 **/
@Component
@EnableScheduling
@Slf4j
public class ProgramTask {

    @Resource
    private TTransmitterProgramMapper tTransmitterProgramMapper;

    @Resource
    TransmitterSendService transmitterSendService;

    @SneakyThrows
    @Scheduled(cron = "0/1 * * * * ? ")
    public void task() {
        List<TTransmitterProgram> list = tTransmitterProgramMapper.selectTTransmitterProgramList(new TTransmitterProgram());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowdate = format.format(new Date()) + " ";
        TTransmitterTask tTransmitterTask;
        for (TTransmitterProgram tTransmitterProgram : list) {
            if ("1".equals(tTransmitterProgram.getFlag())) {
                continue;
            }
            String[] programPeriods = tTransmitterProgram.getProgramPeriod().split(",");
            if (Arrays.stream(programPeriods).anyMatch(s -> s.equals(getWeekOfDate(new Date())))) {
                Date stime = formatter.parse(nowdate + getTimeDate(tTransmitterProgram.getSendDateTime()));
                Date etime = formatter.parse(nowdate + getTimeDate(tTransmitterProgram.getSendDateEndTime()));
                if (stime.compareTo(new Date()) <= 0 && etime.compareTo(new Date()) >= 0) {
                    tTransmitterTask = new TTransmitterTask();
                    BeanUtil.copyProperties(tTransmitterProgram, tTransmitterTask, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                    TTransmitter tnowTTransmitter = transmitterSendService.getTTransmitter(tTransmitterTask.getStationCode(),tTransmitterTask.getTransCode());
                    tTransmitterTask.setTransIp(tnowTTransmitter.getTransIp1());
                    tTransmitterTask.setTransHttpsPort(tnowTTransmitter.getTransHttpsPort());
                    tTransmitterTask.setTransHttpPort(tnowTTransmitter.getTransHttpPort());
                    transmitterSendService.playTask(tTransmitterTask);
                    tTransmitterProgram.setFlag("1");
                    tTransmitterProgramMapper.updateById(tTransmitterProgram);
                }
            }
        }
    }
    @SneakyThrows
    @Scheduled(cron = "0 0 0 * * ? ")
    public void resettask() {
        List<TTransmitterProgram> list = tTransmitterProgramMapper.selectTTransmitterProgramList(new TTransmitterProgram());
        for (TTransmitterProgram tTransmitterProgram : list) {
            tTransmitterProgram.setFlag("0");
            tTransmitterProgramMapper.updateById(tTransmitterProgram);
        }
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public String getWeekOfDate(Date date) {
        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 获取时间时分秒<br>
     *
     * @param date
     * @return 获取时间时分秒
     */
    public String getTimeDate(Date date) {

        // 创建一个Calendar对象，并将Date对象设置为其时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 使用Calendar对象获取小时、分钟和秒
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        return hour + ":" + minute + ":" + second;
    }
}
